const { add, find, update, del,addAll } = require("../../server");
const business = require("../../model/business");
const business1 = require("../../model/business1");

// 查询全部数据
const getCtr = async (req, res, next) => {
  try {
    let data = await find(business);
    res.send({
      code: 200,
      data,
    });
  } catch (err) {
    console.log(err);
    res.send({
      code: 400,
      message: "数据查询失败",
    });
  }
};

// 模糊查询
const getCtr1 = async (req, res, next) => {
  let tiao = req.query;
  try {
    let data = await find(business, {
      nickname: tiao.nickname,
    });
    let data1 = await find(business, {
      approval_status: tiao.approval_status,
    });
    let data2 = await find(business, {
      nickname: tiao.nickname,
      approval_status: tiao.approval_status,
    });
    if (tiao.nickname == "" && tiao.approval_status != "") {
      res.send({
        code: 200,
        data: data1,
      });
    } else {
      if (tiao.nickname != "" && tiao.approval_status == "") {
        res.send({
          code: 200,
          data: data,
        });
      } else {
        res.send({
          code: 200,
          data: data2,
        });
      }
    }
  } catch (err) {
    console.log(err);
    res.send({
      code: 400,
      message: "数据查询失败",
    });
  }
};

// 开启新增
const postCtr = async (req, res, next) => {
  let data = req.body;
  try {
    await add(business, data.newTableDate);
    res.send({
      code: 200,
      message: "添加申请成功",
    });
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: "添加数据失败",
    });
  }
};

// 文件导入
const postCtr1 = async (req, res, next) => {
  let data = req.body;
  try {
    await addAll(business, data);
    res.send({
      code: 200,
      message: "数据插入成功",
    });
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: "数据插入失败",
    });
  }
};

// 信息修改
const putCtr = async (req, res, next) => {
  let data = req.body;
  try {
    let obj = await update(business, data.newTableDate, {
      id: data.id,
    });
    if (obj == 0) {
      res.send({
        code: 400,
        message: "未进行修改操作",
      });
      return;
    }
    res.send({
      code: 200,
      message: "修改成功",
    });
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: "修改失败，请联系超级管理员",
    });
  }
};

// 删除
const delCtr = async (req, res, next) => {
  console.log(req.body);
  try {
    let obj = await del(business, {
      id: req.body.id,
    });
    if (obj == 0) {
      res.send({
        code: 400,
        message: "删除失败，请联系超级管理员",
      });
      return;
    }
    res.send({
      code: 200,
      message: "删除成功",
    });
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: "删除失败，请联系超级管理员",
    });
  }
};
//================================回收站====================================
// 查询全部数据
const getRecycle = async (req, res, next) => {
  try {
    let data = await find(business1);
    res.send({
      code: 200,
      data,
    });
  } catch (err) {
    console.log(err);
    res.send({
      code: 400,
      message: "数据查询失败",
    });
  }
};

// 模糊查询
const getRecycles = async (req, res, next) => {
  let tiao = req.query;
  try {
    let data = await find(business1, {
      nickname: tiao.nickname,
    });
    let data1 = await find(business1, {
      approval_status: tiao.approval_status,
    });
    let data2 = await find(business1, {
      nickname: tiao.nickname,
      approval_status: tiao.approval_status,
    });
    if (tiao.nickname == "" && tiao.approval_status != "") {
      res.send({
        code: 200,
        data: data1,
      });
    } else {
      if (tiao.nickname != "" && tiao.approval_status == "") {
        res.send({
          code: 200,
          data: data,
        });
      } else {
        res.send({
          code: 200,
          data: data2,
        });
      }
    }
  } catch (err) {
    console.log(err);
    res.send({
      code: 400,
      message: "数据查询失败",
    });
  }
};

// 开启新增
const postRecycle = async (req, res, next) => {
  let data = req.body;
  try {
    await add(business1, data);
    res.send({
      code: 200,
      message: "添加申请成功",
    });
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: "添加数据失败",
    });
  }
};

// 删除
const delRecycle = async (req, res, next) => {
  console.log(req.body);
  try {
    let obj = await del(business1, {
      id: req.body.id,
    });
    if (obj == 0) {
      res.send({
        code: 400,
        message: "删除失败，请联系超级管理员",
      });
      return;
    }
    res.send({
      code: 200,
      message: "删除成功",
    });
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: "删除失败，请联系超级管理员",
    });
  }
};

// 恢复
const delRecycles = async (req, res, next) => {
  try {
    let obj = await del(business1, {
      id: req.body.id,
    });
    if (obj == 0) {
      res.send({
        code: 400,
        message: "恢复失败，请联系超级管理员",
      });
      return;
    }
    res.send({
      code: 200,
      message: "恢复成功",
    });
  } catch (error) {
    console.log(error);
    res.send({
      code: 400,
      message: "恢复失败，请联系超级管理员",
    });
  }
};
module.exports = {
  getCtr,
  getCtr1,
  postCtr,
  putCtr,
  delCtr,getRecycle,getRecycles,postRecycle,delRecycle,delRecycles,
  postCtr1
};
